package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class TranController {

    @Autowired
    private DicValueService dicValueService;

    @Autowired
    private UserService userService;

    @Autowired
    private TranService tranService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ContactsService contactsService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private TranRemarkService tranRemarkService;

    @Autowired
    private TranHistoryService tranHistoryService;

    @RequestMapping(value = "workbench/transaction/index.do")
    public String index(HttpServletRequest request){

        List<DicValue> transactionTypeList = dicValueService.queryDicValueByTypeCode("transactionType");
        List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");

        request.setAttribute("transactionTypeList",transactionTypeList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("stageList",stageList);

        return "workbench/transaction/index";
    }

    @RequestMapping(value = "workbench/transaction/toSave.do")
    public String toSave(HttpServletRequest request){

        List<User> userList = userService.queryAllUsers();
        List<DicValue> transactionTypeList = dicValueService.queryDicValueByTypeCode("transactionType");
        List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");

        request.setAttribute("transactionTypeList",transactionTypeList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("stageList",stageList);
        request.setAttribute("userList",userList);
        return "workbench/transaction/save";
    }

    @RequestMapping(value = "workbench/transaction/toEdit.do")
    public String toEdit(String tranId,HttpServletRequest request){
        List<User> userList = userService.queryAllUsers();
        List<DicValue> transactionTypeList = dicValueService.queryDicValueByTypeCode("transactionType");
        List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");

        request.setAttribute("transactionTypeList",transactionTypeList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("stageList",stageList);
        request.setAttribute("userList",userList);
        return "workbench/transaction/edit";
    }

    @RequestMapping(value = "/workbench/transaction/queryTranByConditionForPage.do")
    @ResponseBody
    public Object queryTranByConditionForPage(String owner, String name, String customerName, String stage,
                                              String type, String source, String fullname,int pageNo,
                                              int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("owner",owner);
        map.put("name",name);
        map.put("customerName",customerName);
        map.put("stage",stage);
        map.put("type",type);
        map.put("source",source);
        map.put("fullname",fullname);
        map.put("pageNo",(pageNo - 1) * pageSize);
        map.put("pageSize",pageSize);

        List<Tran> tranList = tranService.queryTranByConditionForPage(map);
        int totalRows = tranService.queryCountOfTranByConditionForPage(map);

        Map<String,Object> result = new HashMap<>();
        result.put("tranList",tranList);
        result.put("totalRows",totalRows);

        return result;
    }

    @RequestMapping(value = "/workbench/transaction/queryActivityByName.do")
    @ResponseBody
    public Object queryActivityByName(String activityName){
        List<Activity> activityList = activityService.queryActivityByName(activityName);
        return activityList;
    }

    @RequestMapping(value = "/workbench/transaction/queryContactsByName.do")
    @ResponseBody
    public Object queryContactsByName(String name){
        List<Contacts> contactsList = contactsService.queryAllContactsByName(name);
        return contactsList;
    }

    @RequestMapping(value = "/workbench/transaction/getPossibilityByStage.do")
    @ResponseBody
    public Object getPossibilityByStage(String stageValue){
        ResourceBundle rb = ResourceBundle.getBundle("possibility");
        String possibility = rb.getString(stageValue);
        return possibility;
    }

    @RequestMapping(value = "/workbench/transaction/queryAllCustomerNameByName.do")
    @ResponseBody
    public Object queryAllCustomerNameByName(String customerName){
        List<String> customerNameList = customerService.queryAllCustomerNameByName(customerName);
        return customerNameList;
    }

    @RequestMapping(value = "/workbench/transaction/saveCreateTran.do")
    @ResponseBody
    public Object saveCreateTran(@RequestParam Map<String,Object> map, HttpSession session){
        ReturnObject returnObject = new ReturnObject();

        map.put(Contants.SESSION_USER,session.getAttribute(Contants.SESSION_USER));

        boolean flag = tranService.saveCreateTran(map);
        if(flag == true){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }else{
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }
        return returnObject;
    }

    @RequestMapping(value = "/workbench/transaction/toDetail.do")
    public String toDetail(String id,HttpServletRequest request){
        Tran tran = tranService.queryTranByIdForDetail(id);
        request.setAttribute("tran",tran);
        List<TranRemark> tranRemarkList =   tranRemarkService.queryTranRemarkForDetailByTranId(id);
        request.setAttribute("tranRemarkList",tranRemarkList);
        List<TranHistory> tranHistoryList = tranHistoryService.queryTranHistoryForDetailByTranId(id);
        request.setAttribute("tranHistoryList",tranHistoryList);
        ResourceBundle rb = ResourceBundle.getBundle("possibility");
        String possibility = rb.getString(tran.getStage());
        tran.setPossibility(possibility);
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");
        request.setAttribute("stageList",stageList);
        return "workbench/transaction/detail";
    }

    @RequestMapping(value = "/workbench/transaction/saveCreateTranRemark.do")
    @ResponseBody
    public Object saveCreateTranRemark(String tranId,String remark,HttpSession session){
        TranRemark tranRemark = new TranRemark();
        User user = (User)session.getAttribute(Contants.SESSION_USER);
        tranRemark.setId(UUIDUtils.getUUID());
        tranRemark.setCreateTime(DateUtils.formatDataTime(new Date()));
        tranRemark.setCreateBy(user.getId());
        tranRemark.setTranId(tranId);
        tranRemark.setNoteContent(remark);

        ReturnObject returnObject = new ReturnObject();
        try {
            int res = tranRemarkService.saveCreateTranRemark(tranRemark);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }
}
